package com.hmjk.health.views;


import com.hmjk.health.R;
import com.hmjk.health.util.BP_Evaluator;
import com.hmjk.health.util.BloodPressureData.BP_DETAIL;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.Rect;
import android.util.AttributeSet;
//import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;


public class BP_GraphView extends RelativeLayout{

	public final static int  MAX_LINES = 6;
	public final static int  MAX_PONITS = 7;
	public final static int  MAX_BP_Evaluator_SUBLEVEL = 10;
	
	private String topTip = null,bottomTip = null;
	
	private ImageView_XY [] mIV=new ImageView_XY[MAX_PONITS];
	
	private View [] mLine_IV=new View[MAX_PONITS];
	
	private View [] mV_LEFTTOP= new  View[MAX_LINES];
	private View [] mV_RIGHTBOTTOM =new  View[MAX_LINES];
	static final int mGridValuelineColor = 0X203a89ff;   //表格线的颜色
	RelativeLayout mrlt;
	int dot_bmp_w,dot_bmp_h;
	
	class BP_Data {
		int sdp;
		int cdp;
		boolean isshow;
		BP_Data(int s, int c, boolean show)
		{
			sdp = s;
			cdp = c; 
			isshow = show;
		}
	}

    private Rect rectGraph=new Rect();	
	
	
	BP_Data[]  tmp_bp_data = new BP_Data[7];
	
	private int [] mIV_RES_ID = {
			R.id.imageView1,
			R.id.imageView2,
			R.id.imageView3,
			R.id.imageView4,
			R.id.imageView5,
			R.id.imageView6,
			R.id.imageView7,			
	};

	private int [] mLINE_IV_RES_ID = {
			R.id.line_imageView1,
			R.id.line_imageView2,
			R.id.line_imageView3,
			R.id.line_imageView4,
			R.id.line_imageView5,
			R.id.line_imageView6,
			R.id.line_imageView7,			
	};	
	
	private int [] mIMAGE_LOC_LEFTTOP = {
			R.id.color_blood_pressure_serious,
			R.id.color_blood_pressure_moderate,
			R.id.color_blood_pressure_mild,
			R.id.color_blood_pressure_normal_high,
			R.id.color_blood_pressure_normal,
			R.id.color_blood_pressure_low,
	 };
	private int [] mIMAGE_LOC_RIGHTBOTTOM = {
			R.id.line_blood_pressure_serious,
			R.id.line_blood_pressure_moderate,
			R.id.line_blood_pressure_mild,
			R.id.line_blood_pressure_normal_high,
			R.id.line_blood_pressure_normal,
			R.id.line_blood_pressure_low,
	 };		
	
	private Rect [] mIMAGE_LOC =  new Rect[MAX_LINES];
	
	private static final int IMAGEVIEW_NO_SELECT = 0; 
	private static final int IMAGEVIEW_SELECTED  = 1; 
	
	private int [] mIV_BACKGROUND_RES_ID = {
			R.drawable.health_today_dot,
			R.drawable.health_dot_seleced
	};	
	


    public interface ICoallBack{  
        public void onClickButton(int i);  
    }  
      
    
    
    
    /** 
     * 初始化接口变量 
     */  
    ICoallBack icallBack = null;  
      
    /** 
     * 自定义控件的自定义事件 
     * @param iBack 接口类型 
     */  
    public void setonClick(ICoallBack iBack)  
    {  
        icallBack = iBack;  
    }  
	
	
	
	public BP_GraphView(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
		reset_bp_data();
        this.setWillNotDraw(false);//必须  
		initView(context);

	}
	
	public BP_GraphView(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
		init(context,attrs);
		reset_bp_data();
        this.setWillNotDraw(false);//必须  
		initView(context);

	}

	public BP_GraphView(Context context, AttributeSet attrs,
			int defStyle) {
		super(context, attrs, defStyle);
		// TODO Auto-generated constructor stub
		init(context,attrs);
        this.setWillNotDraw(false);//必须  
		reset_bp_data();
		initView(context);
		
	}	
	
    private void initView(Context context) {
        // TODO Auto-generated method stub
        View.inflate(context, R.layout.health_card_bp_graph_view, this);
 
	
      for( int i=0;i<7;i++)
      {	  
		 mIV[i] = (ImageView_XY) findViewById(mIV_RES_ID[i]);
      }
  
          
      for( int i=0;i<7;i++)
      {	  
    	  mLine_IV [i] = (View) findViewById(mLINE_IV_RES_ID[i]);
      }      
      
      for( int i=0;i<6;i++)
      {
    	  mV_LEFTTOP[i]  =(View) findViewById(mIMAGE_LOC_LEFTTOP[i]);
      }	
      for( int i=0;i<6;i++)
      {
    	  mV_RIGHTBOTTOM[i]  =(View) findViewById(mIMAGE_LOC_RIGHTBOTTOM[i]);
      }	
  
      mLine_IV[0].setOnTouchListener(new OnTouchListener(){

		@Override
		public boolean onTouch(View v, MotionEvent event) {
			// TODO Auto-generated method stub
            switch (event.getAction()) {
            
            case MotionEvent.ACTION_DOWN:
            {	
			if(null!=icallBack)
			{
				if(mIV[0].isHided()) break;
				setBloodPressureFocus(0);
				if(null!=icallBack)
			    icallBack.onClickButton(0);
			}
            }
            break;
            default:
                break;       	
            }
			return false;

		}
    	  
      });


      mLine_IV[1].setOnTouchListener(new OnTouchListener(){

		@Override
		public boolean onTouch(View v, MotionEvent event) {
			// TODO Auto-generated method stub
            switch (event.getAction()) {
            
            case MotionEvent.ACTION_DOWN:
            {	
			if(null!=icallBack)
			{
				if(mIV[1].isHided()) break;
				setBloodPressureFocus(1);
				if(null!=icallBack)
			    icallBack.onClickButton(1);
			}
            }
            break;
            default:
                break;       	
            }
			return false;

		}
    	  
      });
            
      mLine_IV[2].setOnTouchListener(new OnTouchListener(){

		@Override
		public boolean onTouch(View v, MotionEvent event) {
			// TODO Auto-generated method stub
            switch (event.getAction()) {
            
            case MotionEvent.ACTION_DOWN:
            {	
			if(null!=icallBack)
			{
				if(mIV[2].isHided()) break;
				setBloodPressureFocus(2);
				if(null!=icallBack)
			    icallBack.onClickButton(2);
			}
            }
            break;
            default:
                break;       	
            }
			return false;

		}
    	  
      });
      
      mLine_IV[3].setOnTouchListener(new OnTouchListener(){

		@Override
		public boolean onTouch(View v, MotionEvent event) {
			// TODO Auto-generated method stub
            switch (event.getAction()) {
            
            case MotionEvent.ACTION_DOWN:
            {	
			if(null!=icallBack)
			{
				if(mIV[3].isHided()) break;
				setBloodPressureFocus(3);
				if(null!=icallBack)
			    icallBack.onClickButton(3);
			}
            }
            break;
            default:
                break;       	
            }
			return false;

		}
    	  
      });
      
      mLine_IV[4].setOnTouchListener(new OnTouchListener(){

		@Override
		public boolean onTouch(View v, MotionEvent event) {
			// TODO Auto-generated method stub
            switch (event.getAction()) {
            
            case MotionEvent.ACTION_DOWN:
            {	
			if(null!=icallBack)
			{
				if(mIV[4].isHided()) break;
				setBloodPressureFocus(4);
				if(null!=icallBack)
			    icallBack.onClickButton(4);
			}
            }
            break;
            default:
                break;       	
            }
			return false;

		}
    	  
      });
      mLine_IV[5].setOnTouchListener(new OnTouchListener(){

		@Override
		public boolean onTouch(View v, MotionEvent event) {
			// TODO Auto-generated method stub
            switch (event.getAction()) {
            
            case MotionEvent.ACTION_DOWN:
            {	
			if(null!=icallBack)
			{
				if(mIV[5].isHided()) break;
				setBloodPressureFocus(5);
				if(null!=icallBack)
			    icallBack.onClickButton(5);
			}
            }
            break;
            default:
                break;       	
            }
			return false;

		}
    	  
      });     
      
      mLine_IV[6].setOnTouchListener(new OnTouchListener(){

		@Override
		public boolean onTouch(View v, MotionEvent event) {
			// TODO Auto-generated method stub
            switch (event.getAction()) {
            
            case MotionEvent.ACTION_DOWN:
            {	
			if(null!=icallBack)
			{
				if(mIV[6].isHided()) break;
				setBloodPressureFocus(6);
				if(null!=icallBack)
			    icallBack.onClickButton(6);
			}
            }
            break;
            default:
                break;       	
            }
			return false;

		}
    	  
      });
            
      
      
      ((ImageView_XY)mIV[0]).setOnClickListener(new OnClickListener(){
				@Override
				public void onClick(View arg0) {
					if(null!=icallBack)
					{
						if(mIV[0].isHided()) return;
						setBloodPressureFocus(0);
						if(null!=icallBack)
					    icallBack.onClickButton(0);
					}
				}
		    });
 
      ((ImageView_XY)mIV[1]).setOnClickListener(new OnClickListener(){
				@Override
				public void onClick(View arg0) {
					if(null!=icallBack)
					{
						if(mIV[1].isHided()) return;
						setBloodPressureFocus(1);
						if(null!=icallBack)
					    icallBack.onClickButton(1);
					}
				}
		    });
		 
      ((ImageView_XY)mIV[2]).setOnClickListener(new OnClickListener(){
				@Override
				public void onClick(View arg0) {
					if(null!=icallBack)
					{
						if(mIV[2].isHided()) return;
						setBloodPressureFocus(2);
						if(null!=icallBack)
					    icallBack.onClickButton(2);
					}
				}
		    });

      ((ImageView_XY)mIV[3]).setOnClickListener(new OnClickListener(){
				@Override
				public void onClick(View arg0) {
					if(null!=icallBack)
					{
						if(mIV[3].isHided()) return;
						setBloodPressureFocus(3);
						if(null!=icallBack)
					    icallBack.onClickButton(3);
					}
				}
		    });	
		 
      ((ImageView_XY)mIV[4]).setOnClickListener(new OnClickListener(){
				@Override
				public void onClick(View arg0) {
					if(null!=icallBack)
					{
						if(mIV[4].isHided()) return;
						setBloodPressureFocus(4);
						if(null!=icallBack)
					    icallBack.onClickButton(4);
					}
				}
		    });

      ((ImageView_XY)mIV[5]).setOnClickListener(new OnClickListener(){
				@Override
				public void onClick(View arg0) {
					if(null!=icallBack)
					{
						if(mIV[5].isHided()) return;
						setBloodPressureFocus(5);
						if(null!=icallBack)
					    icallBack.onClickButton(5);
					}
				}
		    });	
      ((ImageView_XY)mIV[6]).setOnClickListener(new OnClickListener(){
				@Override
				public void onClick(View arg0) {
					if(null!=icallBack)
					{
						if(mIV[6].isHided()) return;
						setBloodPressureFocus(6);
						if(null!=icallBack)
					    icallBack.onClickButton(6);
					}
				}
		    });	
		 

	}

    public void clear_BloodPressure_Graph()
	{
    	reset_bp_data();
//		for(int i=0;i<7;i++)
//		{ 
//				((ImageView_XY) mIV[i]).Hide();
//				
//		}
	}    
 
    public void  reset_bp_data()
    {
       	BP_Data d =new BP_Data(0,0,false);
		for(int i=0;i<7;i++)
		{
			tmp_bp_data[i] = d;
		}	
    }
    
    public void setBloodPressureFocus(int j)
	{
		for(int i=0;i<7;i++)
		{   if(j==i)
		    {
			    ((ImageView_XY) mIV[i]).Set_Focus();
		    }
		    else
		    {
				((ImageView_XY) mIV[i]).Set_NoFocus();
		    }

		}
	}
	
	private void init(Context context, AttributeSet attrs)
	{
		if(null != attrs){
 
		}		
	}
	
	public  void setLayout(View view,int x,int y) 
	{ 
	MarginLayoutParams margin=new MarginLayoutParams(view.getLayoutParams()); 
	margin.setMargins(x,y, x+margin.width, y+margin.height); 
	LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(margin); 
	view.setLayoutParams(layoutParams);
//	requestLayout();
	} 


	
	
    public void setBloodPressure(int norder, int nbphigh, int nbplow, boolean bisshow )
    {
    	if(norder>=7) return;
    	if(norder<0) return;
    	{
    		if(bisshow)
    		{
    			BP_Data d = new  BP_Data(nbphigh,nbplow,bisshow);
    			tmp_bp_data[norder] = d;
    		}	
    		
    	}
   	
    }

    
    public int  setBloodPressure(BP_DETAIL[] bp_detail_array )
    {  //如果无值 返回-1; 有则为最后一个有效值;数组不够长返回-2;
    	int rlt = -1;
    	if(bp_detail_array.length<7) return -2;
		for(int i = 0; i < 7; i ++){
			if(bp_detail_array[i] != null){
    			BP_Data d = new  BP_Data(bp_detail_array[i].sbp,bp_detail_array[i].dbp,true);
    			tmp_bp_data[i] = d;
				rlt = i;
			}
			else
			{
    			BP_Data d = new  BP_Data(0,0,false);
    			tmp_bp_data[i] = d;
			}
		}
		return rlt;
    }    
    
    
    public Point Get_BloodPressure_XY(int norder,int nbphigh,int nbplow,Rect rect)
    {
    	//相等情况
    	Point pt=new Point();
    	int h_len,v_len,one_h_len,one_v_len;
    	v_len = rect.height();
       	one_v_len = v_len/(MAX_LINES*MAX_BP_Evaluator_SUBLEVEL);
    	h_len = rect.width();
    	one_h_len = h_len/(MAX_PONITS+1);
    	pt.x =rect.left+one_h_len+norder*one_h_len;
    	BP_Evaluator.Result rlt  = BP_Evaluator.evaluateLevel(nbphigh,nbplow);
    	pt.y=one_v_len*(rlt.type*MAX_BP_Evaluator_SUBLEVEL+rlt.subLevel);
    	return pt;
    }
    
    public Point Get_BloodPressure_Y(int norder,int ntype, int sublevel,Rect rect)
    {
    	//各级高度不一样
    	Point pt=new Point();
    	int h_len,v_len,one_h_len,one_v_len;
    	v_len = rect.height();
    	one_v_len = v_len/(MAX_LINES*MAX_BP_Evaluator_SUBLEVEL);

    	pt.x =(rect.left+rect.right)/2;
    	pt.y= one_v_len*((MAX_LINES - 1 -ntype)*MAX_BP_Evaluator_SUBLEVEL+(MAX_BP_Evaluator_SUBLEVEL - 1 -sublevel));
    	return pt;
    }   
    
    


    
	@Override
	protected void onAttachedToWindow(){
		super.onAttachedToWindow();

	}
	
	@Override
	protected void onDetachedFromWindow(){		

		super.onDetachedFromWindow();
	}	    
	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec){
		
		int widthMode = MeasureSpec.getMode(widthMeasureSpec);
		int widthSize = MeasureSpec.getSize(widthMeasureSpec);	
		int heightMode = MeasureSpec.getMode(heightMeasureSpec);
		int heightSize = MeasureSpec.getSize(heightMeasureSpec);
		



		
		//int size = (widthSize>heightSize)?widthSize:heightSize; 
		
		//int wSpec = MeasureSpec.makeMeasureSpec(widthSize, MeasureSpec.EXACTLY);
		int hSpec = MeasureSpec.makeMeasureSpec(heightSize,heightMode);		
		super.onMeasure(widthMeasureSpec, hSpec);
	}




	
	@Override 
	protected void onDraw(Canvas canvas){
		super.onDraw(canvas);	
        int nstart=-1, nend=-1;
        int abs_left;
        int mIV_width;
        
//        abs_left =mV_LEFTTOP[0].getRight()+(mV_RIGHTBOTTOM[5].getRight()-mV_LEFTTOP[0].getRight()-mLine_IV[6].getRight())/2;
//        abs_left = mV_LEFTTOP[0].getLeft()+mV_RIGHTBOTTOM[5].getRight()
//        		-(mIV[0].getRight()-mIV[0].getLeft())*7;
        mIV_width = mIV[0].getRight()-mIV[0].getLeft();
        abs_left = mV_LEFTTOP[0].getRight()+mV_RIGHTBOTTOM[5].getRight()
        		-mIV_width*7;        
        abs_left = abs_left/2;
        
		Paint mPaint = new Paint();
		mPaint.setColor(mGridValuelineColor);


		for(int i=0;i<7;i++)
		{
			if(tmp_bp_data[i].isshow)
			{
		    	BP_Evaluator.Result rlt  = BP_Evaluator.evaluateLevel(tmp_bp_data[i].sdp,tmp_bp_data[i].cdp);
				rectGraph.left = mIV[i].getLeft();
				rectGraph.top =  mIV[i].getTop();	
				rectGraph.right = mIV[i].getRight();
				rectGraph.bottom = mIV[i].getBottom();
				Point point = Get_BloodPressure_Y(i,rlt.type,rlt.subLevel,rectGraph);
				mIV[i].SetXY((rectGraph.left+rectGraph.right)/2,point.y);

			    nend = nstart;
			    nstart = i;
			}	
			if(-1!=nend)
			{
				int xs1,xe1,ys1,ye1;
 				
				
				xs1 = abs_left+mIV_width*nstart+mIV[nstart].GetX();
				ys1 = mIV[nstart].GetY();
				xe1 = abs_left+mIV_width*nend+mIV[nend].GetX();
				ye1 = mIV[nend].GetY();
 //               Log.e(" XXXX ",nstart+"xs1,xe1,ys1,ye1"+mIV[nstart].getLeft()+"mIV[nstart].getRight()="+mIV[nstart].getRight());
                
				canvas.drawLine( xs1+1,ys1,xe1+1,ye1,mPaint);
				canvas.drawLine( xs1,ys1,xe1,ye1,mPaint);
				canvas.drawLine( xs1-1,ys1,xe1-1,ye1,mPaint);
				canvas.drawLine( xs1,ys1+1,xe1,ye1+1,mPaint);
				canvas.drawLine( xs1+1,ys1+1,xe1+1,ye1+1,mPaint);
				canvas.drawLine( xs1,ys1-1,xe1,ye1-1,mPaint);
				
			}	
		}	

		

	}
}